#include "serialportreadthread.h"
#include <QDebug>
#include "configargs.h"

#if 0
extern "C"
{
	#include <stdio.h>
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <fcntl.h>
	#include <sys/ioctl.h>
	#include <unistd.h>		
	#include <string.h>
	#include <termios.h>
	#include <stdlib.h>
}
#endif

SerialPortReadThread::SerialPortReadThread(QObject *parent) :
    QThread(parent),
    stopped(false)
{
    HexOrChar_flag = 1;  //这里应该是一个标志位,设定十六进制显示或者直接显示字符的,0->16进制显示;          1->字符显示 
}

void SerialPortReadThread::setWeiQianFunctions(SerialPort *serialPort)
{
    m_pSerialPort = serialPort;
}

void SerialPortReadThread::stop()
{
	qDebug("stop!");
    stopped = true;
}

void SerialPortReadThread::run()
{
	qDebug("stopped=%d", stopped);

	
    while(!stopped) 
	{
        QString str ;
        char *pBuff = new char[64];

		
		int size = m_pSerialPort->read(pBuff, 64);
        if(size > 0) 
		{	
//			qDebug("size=%d, pBuff=%s", size, pBuff);
	
            if(receivecont < 18446744073709551610)
           		receivecont += size;

			else
                receivecont=0;
			
			if(!HexOrChar_flag)   
			{
	            for(int i=0; i<size; i++) 
				{
	                QByteArray ba ;

					ba.append(pBuff[i]);        
	                if(ba == 0)
	                    str.append("00"); //在str的后面添加"00"
	                 else
	                    str.append(ba.toHex());
	                if(i != (size))
	                str.append(" ");
	            }
			}
			else
			{	
	            str.append(QByteArray (pBuff, 64));
//	            qDebug()<<str;
			}

			emit receiveSignal(str);
        }
            delete pBuff;
    }
	
    stopped = false;
}






























